<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.cp.vo.CpPostDTO" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.security.UserDetailsServiceImpl" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="com.qipay.baas.model.Cp" %>
<%@ page import="java.util.List" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<!doctype html>
<html>
<head>
    <jsp:include page="head.jsp"></jsp:include>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <% CpPostDTO cpInfo = (CpPostDTO) request.getAttribute("detail");
            Boolean isTest = request.getServerName().indexOf("localhost")>-1;
            Boolean isAdd = String.valueOf(request.getAttribute("isadd")).equals("1");

            Cp proxyCurrentLogin = (Cp)request.getAttribute("agentModel");
            Boolean isProxy = request.getAttribute("agent")!=null&&request.getAttribute("agent").toString().equalsIgnoreCase("1");// proxy!=null&&proxy.getId()!=null;
            UserDetailsServiceImpl.LoginUserDetails userDetails = (UserDetailsServiceImpl.LoginUserDetails) request.getAttribute("admin");
        %>

        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box box-info">
                        <div class="box-header with-border">
                            <h3 class="box-title">编辑商户信息</h3>
                        </div>
                        <!-- /.box-header -->
                        <!-- form start -->
                        <form class="form-horizontal" action="save" method="POST" id="doc-vld-msg">
                            <input type="hidden" name="cpId" value="<%= isAdd?"":String.valueOf(cpInfo.getCpId())%>">
                            <input type="hidden" name="id" value="<%= isAdd?"":String.valueOf(cpInfo.getId())%>">
                            <div class="box-body" id="dvContent">
                                <div class="form-group">
                                    <label for="ddltype" class="col-sm-2 control-label">商户类型：</label>
                                    <div class="col-sm-10">
                                        <select id="ddltype" name="type">
                                            <option value="1">商户</option>
                                            <% if (userDetails.getLevel() == UserLevel.ADMIN) {%>
                                            <option value="2">代理商</option>
                                            <%}%>
                                        </select>
                                    </div>
                                </div>
                                <%if (!isProxy) {%>
                                <div class="form-group">
                                    <label for="txtappId" class="col-sm-2 control-label">分配的APPID：</label>
                                    <div class="col-sm-10"><%= isAdd ? "自动分配" : cpInfo.getAppId()%>
                                        <input type="hidden" class="form-control" id="txtappId" name="appId"
                                               value="<%= isAdd?0:cpInfo.getAppId()%>" placeholder="分配的APPID">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtsecretId" class="col-sm-2 control-label">密钥：</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtsecretId" name="secretId"
                                               mush="1" readonly="readonly"
                                               value="<%= isAdd?request.getAttribute("secretid"):cpInfo.getSecretId()%>"
                                               placeholder="密钥">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtagentId" class="col-sm-2 control-label">代理商ID：</label>
                                    <div class="col-sm-10">
                                        <% if (isAdd&&userDetails.getLevel() == UserLevel.ADMIN) {%>
                                            <select id="ddlAgentId" name="agentId">
                                                <option value="0">无</option>
                                                <% List<Cp> proxies = (List<Cp>)request.getAttribute("proxies");
                                                if(proxies!=null){
                                                    for(Cp proxy : proxies){
                                                        out.print(String.format("<option value=\"%s\">%s</option>",proxy.getId(),proxy.getId()));
                                                    }
                                                }
                                                %>
                                            </select>
                                            <script language="JavaScript">
                                                setTimeout(function () {
                                                    var currentAgentId = '<%= isAdd?"0":cpInfo.getAgentId()%>';
                                                    $("#ddlAgentId").val(currentAgentId);
                                                },100);

                                            </script>
                                        <%} else {%>
                                        <select id="ddlAgentId" name="agentId" readonly>
                                            <option value="<%= isAdd?userDetails.getCpId():cpInfo.getAgentId()%>"><%= isAdd?userDetails.getCpId():cpInfo.getAgentId()%></option>
                                        </select>
                                        <%}%>
                                    </div>
                                </div>
                                <%} else {%>
                                <input type="hidden" class="form-control" id="txtsecretId" name="appId"
                                       value="proxy">
                                <input type="hidden" class="form-control" id="txtsecretId" name="secretId"
                                       value="proxy">
                                <input type="hidden" class="form-control" id="txtagentId" name="agentId"
                                       value="0">
                                <%
                                    }
                                %>
                                <div class="form-group">
                                    <label for="withdrawPassword" class="col-sm-2 control-label">提现密码：</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="withdrawPassword" maxlength="6"
                                               name="withdrawPassword"
                                               mush="1"
                                               regx="^[0-9]{6}$"
                                               value="<%= isAdd?"123456":cpInfo.getWithdrawPassword()%>"
                                               placeholder="提现密码，请输入6位数的数值">
                                    </div>
                                </div>
                                <div class="form-group hidden">
                                    <label for="txtfeeDefaultRecharge"
                                           class="col-sm-2 control-label">手续费百分比(%)：</label>
                                    <div class="col-sm-10">
                                        <input type="number" class="form-control" id="txtfeeDefaultRecharge"
                                               name="feeDefaultRecharge"
                                               mush="1"  maxlength="3"
                                               value="<%= isAdd?"0":cpInfo.getFeeDefaultRecharge()%>"
                                               placeholder="手续费百分比">
                                    </div>
                                </div>
                                <% if(proxyCurrentLogin!=null){
                                //代理商添加 CP时。
                                %>
                                <div class="form-group" >
                                    <label for="txtfeeWithdrawAdvince"
                                           class="col-sm-2 control-label">垫支提现费率(%)：</label>
                                    <div class="col-sm-10">
                                        <input type="number" class="form-control" id="txtfeeWithdrawAdvince"
                                               name="feeWithdrawAdvince"
                                               mush="1"  maxlength="3"
                                               value="<%= isAdd?proxyCurrentLogin.getFeeWithdrawAdvince():cpInfo.getFeeWithdrawAdvince()%>"
                                               placeholder="垫支提现费率"  readonly>
                                        <span class="help-block">垫支提现的费率默认与代理商一致，您无须设置</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtcostWithdraw" class="col-sm-2 control-label">每笔提现费用(元)：</label>
                                    <div class="col-sm-10">
                                        <input type="number" class="form-control" id="txtcostWithdraw"
                                               name="costWithdraw"
                                               mush="1"
                                               value="<%= isAdd?proxyCurrentLogin.getCostWithdraw(): cpInfo.getCostWithdraw()%>"
                                               placeholder="每笔提现费用" readonly>
                                        <span class="help-block">每笔提现的花费默认与代理商一致，您无须设置</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtbindMobile" class="col-sm-2 control-label">手机：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtbindMobile" mush="1"
                                               value="<%= isAdd?"":SiteHelper.getEmptyStr(cpInfo.getBindMobile())%>" placeholder="手机"
                                               name="bindMobile" <%= isAdd?"":"readonly"%>>
                                    </div>
                                </div>
                                <%}else {%>
                                <div class="form-group" >
                                    <label for="txtfeeWithdrawAdvince"
                                           class="col-sm-2 control-label">垫支提现费率(%)：</label>
                                    <div class="col-sm-10">
                                        <input type="number" class="form-control" id="txtfeeWithdrawAdvince"
                                               name="feeWithdrawAdvince"
                                               mush="1"  maxlength="3"
                                               value="<%= isAdd?0:cpInfo.getFeeWithdrawAdvince()%>"
                                               placeholder="垫支提现的费率"  >
                                        <span class="help-block">值须在0-100之间</span>
                                    </div>
                                </div>
                                 <div class="form-group">
                                    <label for="txtcostWithdraw" class="col-sm-2 control-label">每笔提现费用(元)：</label>
                                    <div class="col-sm-10">
                                        <input type="number" class="form-control" id="txtcostWithdraw"
                                               name="costWithdraw"
                                               mush="1"
                                               value="<%= isAdd?"1":cpInfo.getCostWithdraw()%>"
                                               placeholder="每笔提现的花费">
                                        <span class="help-block">须大于0</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtbindMobile" class="col-sm-2 control-label">手机：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtbindMobile" mush="1"
                                               value="<%= isAdd?"":SiteHelper.getEmptyStr(cpInfo.getBindMobile())%>" placeholder="手机"
                                               name="bindMobile">
                                    </div>
                                </div>
                                <%}%>
                                <div class="form-group">
                                    <hr/>
                                </div>
                                <div class="form-group">
                                    <label for="txtname" class="col-sm-2 control-label">企业名称：</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtname" name="name" mush="1"
                                               value="<%= isAdd?"":cpInfo.getName()%>" placeholder="企业名称">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtlegalPerson" class="col-sm-2 control-label">企业法人：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtlegalPerson" name="legalPerson"
                                               value="<%= isAdd?"":cpInfo.getLegalPerson()%>" mush="1"
                                               placeholder="法人代表">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="doc-ipt-file-1" class="col-sm-2 control-label">法人身份证正面：
                                    </label>

                                    <div class="col-sm-10">
                                        <img id="imgIcon1"
                                             class="img-polaroid"
                                             src="<%= isAdd?"":cpInfo.getLegalPersonIdcard1()%>" width="140"
                                        />
                                        <input name="file" type="file" class="form-control" id="doc-ipt-file-1"
                                               placeholder="法人身份证正面图片"
                                               onchange="PostImg2(this,'imgIcon1')">
                                        <input type="hidden"
                                               value="<%= isAdd?"":cpInfo.getLegalPersonIdcard1()%>" mush="<%= isTest?0:1%>"
                                               id="hdimgIcon1" name="legalPersonIdcard1" placeholder="法人身份证正面图片">

                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="doc-ipt-file-2" class="col-sm-2 control-label">法人身份证反面：
                                    </label>
                                    <div class="col-sm-10">
                                        <img id="imgIcon2"
                                             class="img-polaroid"
                                             src="<%= isAdd?"":cpInfo.getLegalPersonIdcard2()%>" width="140"
                                        />
                                        <input name="file" type="file" class="form-control" id="doc-ipt-file-2"
                                               placeholder="法人身份证反面图片"
                                               onchange="PostImg2(this,'imgIcon2')">
                                        <input type="hidden"
                                               value="<%= isAdd?"":cpInfo.getLegalPersonIdcard2()%>" mush="<%= isTest?0:1%>"
                                               id="hdimgIcon2" name="legalPersonIdcard2" placeholder="法人身份证反面图片">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="doc-ipt-file-3" class="col-sm-2 control-label">企业营业执照：
                                    </label>

                                    <div class="col-sm-10">
                                        <img id="imgIcon3"
                                             class="img-polaroid"
                                             src="<%= isAdd?"":cpInfo.getBusinessLicensePic()%>" width="140"
                                        />
                                        <input name="file" type="file" class="form-control" id="doc-ipt-file-3"
                                               placeholder="企业营业执照照片"
                                               onchange="PostImg2(this,'imgIcon3')">
                                        <input type="hidden"
                                               value="<%= isAdd?"":cpInfo.getBusinessLicensePic()%>" mush="<%= isTest?0:1%>"
                                               id="hdimgIcon3" name="businessLicensePic" placeholder="企业营业执照照片">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="doc-ipt-file-4" class="col-sm-2 control-label">银行卡：
                                    </label>

                                    <div class="col-sm-10">
                                        <img id="imgIcon4"
                                             class="img-polaroid"
                                             src="<%= isAdd?"":cpInfo.getOrgCodeLicense()%>" width="140"
                                        />
                                        <input name="file" type="file" class="form-control" id="doc-ipt-file-4"
                                               placeholder="银行卡照片"
                                               onchange="PostImg2(this,'imgIcon4')">
                                        <input type="hidden"
                                               value="<%= isAdd?"":cpInfo.getOrgCodeLicense()%>" mush="<%= isTest?0:1%>"
                                               id="hdimgIcon4" name="orgCodeLicense" placeholder="银行卡">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtbusinessLicenseNo" class="col-sm-2 control-label">企业营业执照注册号：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtbusinessLicenseNo" mush="1"
                                               name="businessLicenseNo"
                                               value="<%= isAdd?"":cpInfo.getBusinessLicenseNo()%>"
                                               placeholder="企业营业执照注册号">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="txtmail" class="col-sm-2 control-label">企业邮箱：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="email" class="form-control" id="txtmail" mush="1"
                                               value="<%= isAdd?"":cpInfo.getMail()%>" placeholder="邮箱" name="mail">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtaddr" class="col-sm-2 control-label">公司地址：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtaddr" mush="1"
                                               value="<%= isAdd?"":cpInfo.getAddr()%>" placeholder="地址" name="addr">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txturl" class="col-sm-2 control-label">公司网站：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txturl" mush="1"
                                               value="<%= isAdd?"":cpInfo.getUrl()%>" placeholder="网站" name="url">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtindustry" class="col-sm-2 control-label">所属行业：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtindustry" mush="1"
                                               value="<%= isAdd?"":cpInfo.getIndustry()%>" placeholder="所属行业"
                                               name="industry">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtcontacts" class="col-sm-2 control-label">联系人：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtcontacts" mush="1"
                                               value="<%= isAdd?"":cpInfo.getContacts()%>" placeholder="公司联系人"
                                               name="contacts">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtcontactsTel" class="col-sm-2 control-label">联系人电话：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtcontactsTel" mush="1"
                                               value="<%= isAdd?"":cpInfo.getContactsTel()%>" placeholder="企业联系人电话"
                                               name="contactsTel">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="txtcontactsMobile" class="col-sm-2 control-label">联系人手机：
                                    </label>

                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="txtcontactsMobile" mush="1"
                                               value="<%= isAdd?"":cpInfo.getContactsMobile()%>" placeholder="企业联系人手机"
                                               name="contactsMobile">
                                    </div>
                                </div>
                            </div>
                            <!-- /.box-body -->
                            <div class="box-footer">
                                <button type="button" class="btn btn-default" onclick="window.history.back()">返回
                                </button>
                                <button type="button" class="button back-gray pull-right" id="btnSearch">确 定</button>
                            </div>
                            <!-- /.box-footer -->
                        </form>
                    </div>
                    <!-- /.box -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <jsp:include page="foot.jsp"></jsp:include>
</div>
<script type="text/javascript"
        src="<%= request.getAttribute("StaticHost") %>/js/jquery.ajaxfileupload.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/fastclick.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script>
    $(function () {
        var currentType = '<%= request.getAttribute("isadd")%>';
        if (currentType == "1") {
            currentType =
            <%= request.getParameter("agent")!=null?"2":"1"%>
        }
        else {
            currentType = <%= cpInfo!=null&&cpInfo.getType()!=null?cpInfo.getType().byteValue():1%>;
        }
        if (currentType >= 0) {
            $("#ddltype").val(currentType);
        }

        $("#btnSearch").on("click", function () {
            var isGo = checkInputMust("dvContent");
            if (isGo) {
                var m = $("#txtbindMobile").val();
                var resm = /^0?(13|15|17|18)[0-9]{9}$/;
                if (m != "" && !resm.test(m)) {
                    alert("手机号必须为数字。");
                    return;
                }
                m = $("#txtcontactsMobile").val();
                if (m != "" && !resm.test(m)) {
                    alert("企业联系人手机号必须为数字。");
                    return;
                }
                var advince = parseInt($("#txtfeeWithdrawAdvince").val(), 10);
                if (advince < 0 || advince > 100) {
                    alert("垫支到账比值要在0-100之间");
                    return;
                }
                var v = parseInt($("#txtcostWithdraw").val());
                if(v<=0){
                    alert("每笔提现的花费值要大于0");
                    return;
                }
                v = parseInt(v * 100, 10);
                $("#txtcostWithdraw").val(v);

                //alert("testing.");
                $("#doc-vld-msg")[0].submit();
            }

        });

    });

    function PostImg2(selectfile, imgPreviewId) {
        var img = $.trim($(selectfile).val());
        if (img == "") {
            alert("请选择图片");
            return;
        }
        if (img) {
            var len1 = img.lastIndexOf(".");
            var ext = img.substr(len1, img.length - len1).toLowerCase();
            var arrow = ".jpg.jpeg.gif.png";
            if (arrow.indexOf(ext) > -1) {
                $.ajaxFileUpload({
                    url: '${pageContext.request.contextPath}/ajax/upload?from=card&r=' + Math.random(),
                    secureuri: false,
                    fileElementId: selectfile.id, dataType: 'json',
                    success: function (data) {
                        if (data && data.code == "200") {
                            $("#hd" + imgPreviewId).val(data.data);
                            $("#" + imgPreviewId).attr("src", data.data);
                            $("#" + imgPreviewId).show();
                        }
                        else {
                            alert("图片上传失败");
                        }
                    },
                    error: function (data, status, e) {
                        alert("error:" + data);
                    }
                });
            }
            else {
                alert("文件格式错误");
                return;
            }
        }

    }
</script>
</body>
</html>

